from farstyle.models import CSS, JS
from base import BaseTest

from django.conf import settings
from django.template import Template, Context, TemplateSyntaxError

import unittest
import os
import re

def render_template(source):
    context = Context({})
    source = '{% load farstyle_tags %}' + source
    return Template(source).render(context)

class CSSTagTest(BaseTest):
    def parse_tag(self, tag):
        m = re.match(u'<link rel="stylesheet" type="text/css" href="/static/([^"]+)" media="([^"]+)" />', tag)
        return {
            'path': m.group(1),
            'media': m.group(2),
        }

    def testRender(self):
        src = '{% css_assets %}'
        output = render_template(src)
        tags = output.split('\n')
        self.assertEqual(len(tags), 2)
        self.assertEqual(self.parse_tag(tags[0])['media'],'all')
        self.assertEqual(self.parse_tag(tags[1])['media'], 'screen')

        src = '{% css_assets "IE" %}'
        output = render_template(src)
        tags = output.split('\n')
        self.assertEqual(len(tags), 1)
        self.assertEqual(self.parse_tag(tags[0])['media'],'all')

class JSTagTest(BaseTest):
    def parse_tag(self, tag):
        m = re.match(u'<script type="text/javascript" src="/static/([^"]+)"></script>', tag)
        return m.group(1)

    def testRender(self):
        src = '{% js_assets %}'
        output = render_template(src)
        tags = output.split('\n')
        self.assertEqual(len(tags), 1)
        path = self.parse_tag(tags[0])
        self.assertEqual(path[:15], '#assets#/asset_')

templatetags_tests = r"""
"""
